<style scoped src="./index.scss" lang="scss" rel="stylesheet/scss"></style>
<template src="./index.html"></template>
<script>
export default {
  mixins: [Vue.prototype.$vFN.Mixins.Base],
  data() {
    return {
      form: {
        isOpenRecharge: 0, //是否开启充值
        moneyMark: "", //余额文案
        isLimitRecharge: 0, //是否限制充值
        isOpenMaxAmount: 0, //是否开启最大金额限制
        isOpenMinAmount: 0, //是否开启最小金额限制

        isOpenFixed: 0, //是否开启固定金额
        isOpenCustom: 0, //是否开启自定义金额

        isOpenWithdraw: 0, // 是否开启提现
        isOpenAutoAudit: 0, //是否开启自动审核
        isOpenAutoPay: 0, //是否开启自动打款

        minAmount: "", //最小限制金额
        maxAmount: "", //最大限制金额

        minWithdrawAmount: "", //最低提现金额
        withdrawFee: "", //提现手续费

        minExemptFee: "", //免除手续费起始金额
        maxExemptFee: "", //免除手续费终止金额

        supportBankLst: [], //支持银行卡的银行列表
        fixedAmountLst: [], //固定充值的金额

        moneySettingItemLst: [], //	提现方式设置
      },

      apiUrl: {
        detail: "mall-site-portal/member-money-setting/getItem",
        update: "mall-site-portal/member-money-setting/saveOrUpdate",
      },

      dialogVisible: false, //添加金额弹窗显隐
      addAmount: "",
      bankInput: "",

      load: true,
    };
  },
  methods: {
    //   保存设置
    async submit() {
      if (!(await this.validate())) {
        return;
      }

      const {
        isOpenRecharge,
        isOpenFixed,
        isOpenWithdraw,
        supportBankLst,
        fixedAmountLst,
        moneySettingItemLst,
      } = this.form;

      // 如果开启充值
      if (isOpenRecharge === 1) {
        if (isOpenFixed === 1 && fixedAmountLst.length === 0) {
          this.$vFN.Tools.messageError("至少添加一种固定充值金额");
          return;
        }
      }

      // 如果开启提现
      if (isOpenWithdraw === 1) {
        let testArr = moneySettingItemLst.filter((item) => {
          return item.isOpen === 1;
        });

        // 如果一个提现方式都没选
        if (this.$vFN.Utils.isEmpty(testArr)) {
          this.$vFN.Tools.messageError("至少选择一种提现方式");
          return;
        }

        if (supportBankLst.length === 0) {
          this.$vFN.Tools.messageError("至少添加一家银行");
          return;
        }
      }

      this.load = true;
      let res = await this.$vFN.Http.post(
        {
          url: this.apiUrl.update,
          data: {
            ...this.form,
          },
        },
        window
      );

      if (res._ok) {
        this.load = false;
        this.$vFN.Tools.messageSuccess(res.message);
        this.getSetting();
      }
    },

    init() {
      this.getSetting();
    },

    async getSetting() {
      this.load = true;
      let res = await this.$vFN.Http.post(
        {
          url: this.apiUrl.detail,
        },
        window
      );

      console.log(res);
      if (res._ok) {
        let data_ = res.data;

        for (const key in this.form) {
          if (!this.$vFN.Utils.isEmpty(data_[key])) {
            this.form[key] = data_[key];
          }
        }

        this.load = false;
      }
    },

    // 添加固定金额
    addFixed() {
      this.form.fixedAmountLst.push(this.addAmount);
      this.addAmount = "";
      this.dialogVisible = false;
    },

    // 删除固定金额tag
    deleteTagAmount(index) {
      this.form.fixedAmountLst.splice(index, 1);
    },

    // 添加银行
    addBank() {
      this.form.supportBankLst.push(this.bankInput);
      this.bankInput = "";
    },

    // 删除银行tag
    deleteTagBank(index) {
      this.form.supportBankLst.splice(index, 1);
    },

    // 表单验证方法 =================
    // 提现手续费
    validator(rule, value, callback) {
      if (value > 100) {
        callback(new Error("最大比例为100%"));
      } else {
        this.$vFN.Tools.validateMoney(rule, value, callback);
      }
    },

    // 最低提现额度
    validatorMinWithdrawAmount(rule, value, callback) {
      if (value === "") {
        callback(new Error("不能为空"));
      } else {
        this.$vFN.Tools.validateStock(rule, value, callback);
      }
    },

    // 余额文字
    validatorMoneyMark(rule, value, callback) {
      if (value === "" && this.form.isOpenRecharge === 1) {
        callback(new Error("不能为空"));
      } else {
        callback();
      }
    },

    // 最小充值金额
    validatorMinAmount(rule, value, callback) {
      if (this.form.isLimitRecharge === 1) {
        if (value === "" && this.form.isOpenMinAmount === 1) {
          callback(new Error("不能为空"));
        } else if (this.form.isOpenMinAmount === 0) {
          this.form.minAmount = "";
          callback();
        } else {
          this.$vFN.Tools.validateStock(rule, value, callback);
        }
      } else {
        callback();
      }
    },

    // 最大充值金额
    validatorMaxAmount(rule, value, callback) {
      // 判断限制充值是否开启
      if (this.form.isLimitRecharge === 1) {
        if (value === "" && this.form.isOpenMaxAmount === 1) {
          callback(new Error("不能为空"));
        } else if (this.form.isOpenMaxAmount === 0) {
          this.form.maxAmount = "";
          callback();
        } else {
          this.$vFN.Tools.validateStock(rule, value, callback);
        }
      } else {
        callback();
      }
    },

    // 免手续费区间
    validatorMinExemptFee(rule, value, callback) {
      if (value === "" || this.form.maxExemptFee === "") {
        callback(new Error("两项必填"));
      } else if (value > this.form.maxExemptFee) {
        callback(new Error("最大区间不得小于最小区间"));
      } else if (value == this.form.maxExemptFee) {
        callback(new Error("最大区间不得等于最小区间"));
      } else {
        callback();
      }
    },

    validate() {
      let that = this;
      return new Promise((resolve, reject) => {
        that.$refs["editForm"].validate((valid) => {
          if (valid) {
            resolve(true);
          } else {
            resolve(false);
          }
        });
      });
    },

    //表单验证结束 ===========
  },
  mounted() {
    this.init();
  },
};
</script>
